﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace CADDemo2025
{
    public class Command0207
    {
        Circle _circle = new Circle();
        Line _line1 = new Line();
        Line _line2 = new Line();
        [CommandMethod("Demo02071")]
        public void Demo02071()
        {
            if (!UmUserMethod.GetEntity(out _circle)) return;
            double r = _circle.Radius;
            double x = _circle.Center.X;
            double y = _circle.Center.Y;
            _line1 = new Line(new Point3d(x - r * 1.1, y, 0), new Point3d(x + r * 1.1, y, 0));
            _line2 = new Line(new Point3d(x, y - r * 1.1, 0), new Point3d(x, y + r * 1.1, 0));
            _line1.AddEntity();
            _line2.AddEntity();
            _circle.EditEntity(() =>
            {
                _circle.Modified -= Circle_Modified;
                _circle.Modified += Circle_Modified;
            });
        }

        private void Circle_Modified(object sender, EventArgs e)
        {
            double r = _circle.Radius;
            double x = _circle.Center.X;
            double y = _circle.Center.Y;
            _line1.EditEntity(() =>
            {
                _line1.StartPoint = new Point3d(x - r * 1.1, y, 0);
                _line1.EndPoint = new Point3d(x + r * 1.1, y, 0);
            });
            _line2.EditEntity(() =>
            {
                _line2.StartPoint = new Point3d(x, y - r * 1.1, 0);
                _line2.EndPoint = new Point3d(x, y + r * 1.1, 0);
            });
        }
        Polyline _pl = new Polyline();
        DBText _text = new DBText();
        [CommandMethod("Demo02072")]
        public void Demo02072()
        {
            if (!UmUserMethod.GetEntity(out _pl)) return;
            double h = _pl.GeometricExtents.MaxPoint.Y - _pl.GeometricExtents.MinPoint.Y;
            _text.TextString = GetStr();
            _text.Height = h / 5.0;
            _text.Position = _pl.GeometricExtents.MaxPoint;
            _text.AddEntity();
            _pl.EditEntity(() =>
            {
                _pl.Modified -= Pl_Modified;
                _pl.Modified += Pl_Modified;
            });
        }

        private void Pl_Modified(object sender, EventArgs e)
        {
            _text.EditEntity(() =>
            {
                double h = _pl.GeometricExtents.MaxPoint.Y - _pl.GeometricExtents.MinPoint.Y;
                _text.TextString = GetStr();
                _text.Height = h / 5.0;
                _text.Position = _pl.GeometricExtents.MaxPoint;
            });
        }

        private string GetStr()
        {
            string str = "";
            for (int i = 1; i < _pl.NumberOfVertices; i++)
            {
                Point3d p1 = _pl.GetPoint3dAt(i - 1);
                Point3d p2 = _pl.GetPoint3dAt(i);
                double dis = _pl.GetDistAtPoint(p2) - _pl.GetDistAtPoint(p1);
                if (str != "") str += ",";
                str += dis.ToString("0.01");
            }
            return str;
        }
    }
}
